// pages/list/index.js
import {__get, __delete} from '../../service/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    offset: 0,
    range: 15,
    total: 0,
    showMore: false,
    noMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDataList();
  },
  //get data list
  getDataList () {
    const __this = this;
    const params = {
      offset: this.data.offset * this.data.range,
      range: this.data.range
    }
    __get('lists', params, res => {
      let data = this.data.offset == 0 ? res.items : this.data.dataList.concat(res.items);
      __this.setData({
        dataList: data,
        total: res.total,
        showMore: false
      })
      wx.stopPullDownRefresh();
    })
  },
  // cell on Click
  cellOnClick(e) {
    const id = e.target.id;
    const __this = this;
    wx.showActionSheet({
      itemList: ['导出到 Word', '查看/编辑', '删除'],
      success: res => {
        if (res.tapIndex === 0) {
          wx.setClipboardData({
            data: getApp().data.baseUrl + 'word/' + id,
            success: res => {
              wx.showToast({
                title: 'word链接已经复制到剪贴板',
                icon: 'none'
              })
            }
          })
        } else if (res.tapIndex === 1) {
          wx.navigateTo({
            url: '/pages/form/index?id=' + id,
          })
        } else if (res.tapIndex === 2) {
          wx.showModal({
            title: '警告：',
            content: '确定要删除这个调查么？',
            confirmText: '确定',
            confirmColor: '#ff0000',
            success: res => {
              if (res.confirm) {
                __delete('lists/' + id, res => {
                  __this.getDataList()
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      offset: 0
    });
    this.getDataList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const offset = (this.data.offset + 1) * this.data.range;
    this.setData({showMore: true});
    if (offset > this.data.total) {
      this.setData({
        showMore: false,
        noMore: true
      })
      return false;
    } else {
      this.setData({
        offset: this.data.offset+1
      });
      this.getDataList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})